package org.bite.lesson13;

import java.util.concurrent.TimeUnit;

/**
 * 死锁的演示
 */
public class Demo1302 {
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(()->{
            System.out.println("t1申请locker1……");
            synchronized(locker1){
                System.out.println("t1获取到了locker1");
                //模拟业务执行时间
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //在持有locker1的基础上获取locker2
                synchronized(locker2){
                    System.out.println("t1获取到了locker2");
                }
            }
        });

        Thread t2 = new Thread(()->{
            System.out.println("t2申请locker2……");
            synchronized(locker2){
                System.out.println("t2获取到了locker2");
                //模拟业务执行时间
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //在持有locker1的基础上获取locker2
                synchronized(locker1){
                    System.out.println("t2获取到了locker1");
                }
            }
        });

        t1.start();
        t2.start();

    }
}
